home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / gnu_oleo_1_2_2.lha / oleo-1.2.2 / _doprnt.c next >
C/C++ Source or Header  |  1993-03-03  |  17KB  |  714 lines

  1. /*
  2.  * Copyright (c) 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)doprnt.c    5.35 (Berkeley) 6/27/88";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. #include <sys/types.h>
  23. #include <varargs.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26.  
  27. #ifdef SYSV
  28. typedef unsigned char u_char;
  29. typedef unsigned long u_long;
  30. #endif
  31.  
  32. /* 11-bit exponent (VAX G floating point) is 308 decimal digits */
  33. #define    MAXEXP        308
  34. /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */
  35. #define    MAXFRACT    39
  36.  
  37. #define    DEFPREC        6
  38.  
  39. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  40.  
  41. #define    PUTC(ch)    (void) putc(ch, fp)
  42.  
  43. #define    ARG() \
  44.     _ulong = flags&LONGINT ? va_arg(argp, long) : \
  45.         flags&SHORTINT ? va_arg(argp, short) : va_arg(argp, int);
  46.  
  47. #define    todigit(c)    ((c) - '0')
  48. #define    tochar(n)    ((n) + '0')
  49.  
  50. /* have to deal with the negative buffer count kludge */
  51. #define    NEGATIVE_COUNT_KLUDGE
  52.  
  53. #define    LONGINT        0x01        /* long integer */
  54. #define    LONGDBL        0x02        /* long double; unimplemented */
  55. #define    SHORTINT    0x04        /* short integer */
  56. #define    ALT        0x08        /* alternate form */
  57. #define    LADJUST        0x10        /* left adjustment */
  58. #define    ZEROPAD        0x20        /* zero (as opposed to blank) pad */
  59. #define    HEXPREFIX    0x40        /* add 0x or 0X prefix */
  60.  
  61. extern double __plinf, __neinf, __nan;
  62. /* double __plinf = 1e500; */
  63. char *__inf_str = "#INF";
  64.  
  65. /* double __neinf = -1e500; */
  66. char *__ninf_str = "#NINF";
  67.  
  68. /* __asm(".globl ___nan");
  69. __asm("___nan: .double 0rnan"); */
  70. /* double __nan = __asm("0rnan"); */
  71. char *__nan_str = "#NAN";
  72.  
  73. static int cvt();
  74. static char *round();
  75. static char *exponent();
  76.  
  77. extern double modf();
  78. extern int strlen();
  79. extern void bcopy();
  80.  
  81. int
  82. _doprnt(fmt0, argp, fp)
  83.     u_char *fmt0;
  84.     va_list argp;
  85.     register FILE *fp;
  86. {
  87.     register u_char *fmt;    /* format string */
  88.     register int ch;    /* character from fmt */
  89.     register int cnt;    /* return value accumulator */
  90.     register int n;        /* random handy integer */
  91.     register char *t;    /* buffer pointer */
  92.     double _double;        /* double precision arguments %[eEfgG] */
  93.     u_long _ulong;        /* integer arguments %[diouxX] */
  94.     int base;        /* base for [diouxX] conversion */
  95.     int dprec;        /* decimal precision in [diouxX] */
  96.     int fieldsz;        /* field size expanded by sign, etc */
  97.     int flags;        /* flags as above */
  98.     int fpprec;        /* `extra' floating precision in [eEfgG] */
  99.     int prec;        /* precision from format (%.3d), or -1 */
  100.     int realsz;        /* field size expanded by decimal precision */
  101.     int size;        /* size of converted field or string */
  102.     int width;        /* width from format (%8d), or 0 */
  103.     char sign;        /* sign prefix (' ', '+', '-', or \0) */
  104.     char softsign;        /* temporary negative sign for floats */
  105.     char *digs;        /* digits for [diouxX] conversion */
  106.     char buf[BUF];        /* space for %c, %[diouxX], %[eEfgG] */
  107.  
  108.     if (fp->_flag & _IORW) {
  109.         fp->_flag |= _IOWRT;
  110.         fp->_flag &= ~(_IOEOF|_IOREAD);
  111.     }
  112.     if ((fp->_flag & _IOWRT) == 0)
  113.         return (EOF);
  114.  
  115.     fmt = fmt0;
  116.     digs = "0123456789abcdef";
  117.     for (cnt = 0;; ++fmt) {
  118.         n = fp->_cnt;
  119.         for (t = (char *)fp->_ptr; (ch = *fmt) && ch != '%';
  120.              ++cnt, ++fmt)
  121.             if (--n < 0
  122. #ifdef NEGATIVE_COUNT_KLUDGE
  123.                 && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz)
  124. #endif
  125.                 || ch == '\n' && fp->_flag & _IOLBF) {
  126.                 fp->_cnt = n;
  127.                 fp->_ptr = t;
  128.                 (void) _flsbuf((u_char)ch, fp);
  129.                 n = fp->_cnt;
  130.                 t = (char *)fp->_ptr;
  131.             } else
  132.                 *t++ = ch;
  133.         fp->_cnt = n;
  134.         fp->_ptr = t;
  135.         if (!ch)
  136.             return (cnt);
  137.  
  138.         flags = 0; dprec = 0; fpprec = 0; width = 0;
  139.         prec = -1;
  140.         sign = '\0';
  141.  
  142. rflag:        switch (*++fmt) {
  143.         case ' ':
  144.             /*
  145.              * ``If the space and + flags both appear, the space
  146.              * flag will be ignored.''
  147.              *    -- ANSI X3J11
  148.              */
  149.             if (!sign)
  150.                 sign = ' ';
  151.             goto rflag;
  152.         case '#':
  153.             flags |= ALT;
  154.             goto rflag;
  155.         case '*':
  156.             /*
  157.              * ``A negative field width argument is taken as a
  158.              * - flag followed by a  positive field width.''
  159.              *    -- ANSI X3J11
  160.              * They don't exclude field widths read from args.
  161.              */
  162.             if ((width = va_arg(argp, int)) >= 0)
  163.                 goto rflag;
  164.             width = -width;
  165.             /* FALLTHROUGH */
  166.         case '-':
  167.             flags |= LADJUST;
  168.             goto rflag;
  169.         case '+':
  170.             sign = '+';
  171.             goto rflag;
  172.         case '.':
  173.             if (*++fmt == '*')
  174.                 n = va_arg(argp, int);
  175.             else {
  176.                 n = 0;
  177.                 while (isascii(*fmt) && isdigit(*fmt))
  178.                     n = 10 * n + todigit(*fmt++);
  179.                 --fmt;
  180.             }
  181.             prec = n < 0 ? -1 : n;
  182.             goto rflag;
  183.         case '0':
  184.             /*
  185.              * ``Note that 0 is taken as a flag, not as the
  186.              * beginning of a field width.''
  187.              *    -- ANSI X3J11
  188.              */
  189.             flags |= ZEROPAD;
  190.             goto rflag;
  191.         case '1': case '2': case '3': case '4':
  192.         case '5': case '6': case '7': case '8': case '9':
  193.             n = 0;
  194.             do {
  195.                 n = 10 * n + todigit(*fmt);
  196.             } while (isascii(*++fmt) && isdigit(*fmt));
  197.             width = n;
  198.             --fmt;
  199.             goto rflag;
  200.         case 'L':
  201.             flags |= LONGDBL;
  202.             goto rflag;
  203.         case 'h':
  204.             flags |= SHORTINT;
  205.             goto rflag;
  206.         case 'l':
  207.             flags |= LONGINT;
  208.             goto rflag;
  209.         case 'c':
  210.             *(t = buf) = va_arg(argp, int);
  211.             size = 1;
  212.             sign = '\0';
  213.             goto pforw;
  214.         case 'D':
  215.             flags |= LONGINT;
  216.             /*FALLTHROUGH*/
  217.         case 'd':
  218.         case 'i':
  219.             ARG();
  220.             if ((long)_ulong < 0) {
  221.                 _ulong = -_ulong;
  222.                 sign = '-';
  223.             }
  224.             base = 10;
  225.             goto number;
  226.         case 'e':
  227.         case 'E':
  228.         case 'f':
  229.         case 'g':
  230.         case 'G':
  231.             _double = va_arg(argp, double);
  232.             /*
  233.              * don't do unrealistic precision; just pad it with
  234.              * zeroes later, so buffer size stays rational.
  235.              */
  236.             if (prec > MAXFRACT) {
  237.                 if (*fmt != 'g' && *fmt != 'G' || (flags&ALT))
  238.                     fpprec = prec - MAXFRACT;
  239.                 prec = MAXFRACT;
  240.             }
  241.             else if (prec == -1)
  242.                 prec = DEFPREC;
  243.             /*
  244.              * softsign avoids negative 0 if _double is < 0 and
  245.              * no significant digits will be shown
  246.              */
  247.             if (_double < 0) {
  248.                 softsign = '-';
  249.                 _double = -_double;
  250.             }
  251.             else
  252.                 softsign = 0;
  253.             /*
  254.              * cvt may have to round up past the "start" of the
  255.              * buffer, i.e. ``intf("%.2f", (double)9.999);'';
  256.              * if the first char isn't NULL, it did.
  257.              */
  258.             *buf = NULL;
  259.             size = cvt(_double, prec, flags, &softsign, *fmt, buf,
  260.                 buf + sizeof(buf));
  261.             if (softsign)
  262.                 sign = '-';
  263.             t = *buf ? buf : buf + 1;
  264.             goto pforw;
  265.         case 'n':
  266.             if (flags & LONGINT)
  267.                 *va_arg(argp, long *) = cnt;
  268.             else if (flags & SHORTINT)
  269.                 *va_arg(argp, short *) = cnt;
  270.             else
  271.                 *va_arg(argp, int *) = cnt;
  272.             break;
  273.         case 'O':
  274.             flags |= LONGINT;
  275.             /*FALLTHROUGH*/
  276.         case 'o':
  277.             ARG();
  278.             base = 8;
  279.             goto nosign;
  280.         case 'p':
  281.             /*
  282.              * ``The argument shall be a pointer to void.  The
  283.              * value of the pointer is converted to a sequence
  284.              * of printable characters, in an implementation-
  285.              * defined manner.''
  286.              *    -- ANSI X3J11
  287.              */
  288.             /* NOSTRICT */
  289.             _ulong = (u_long)va_arg(argp, void *);
  290.             base = 16;
  291.             goto nosign;
  292.         case 's':
  293.             if (!(t = va_arg(argp, char *)))
  294.                 t = "(null)";
  295.             if (prec >= 0) {
  296.                 /*
  297.                  * can't use strlen; can only look for the
  298.                  * NUL in the first `prec' characters, and
  299.                  * strlen() will go further.
  300.                  */
  301.                 char *p, *memchr();
  302.  
  303.                 if (p = memchr(t, 0, prec)) {
  304.                     size = p - t;
  305.                     if (size > prec)
  306.                         size = prec;
  307.                 } else
  308.                     size = prec;
  309.             } else
  310.                 size = strlen(t);
  311.             sign = '\0';
  312.             goto pforw;
  313.         case 'U':
  314.             flags |= LONGINT;
  315.             /*FALLTHROUGH*/
  316.         case 'u':
  317.             ARG();
  318.             base = 10;
  319.             goto nosign;
  320.         case 'X':
  321.             digs = "0123456789ABCDEF";
  322.             /* FALLTHROUGH */
  323.         case 'x':
  324.             ARG();
  325.             base = 16;
  326.             /* leading 0x/X only if non-zero */
  327.             if (flags & ALT && _ulong != 0)
  328.                 flags |= HEXPREFIX;
  329.  
  330.             /* unsigned conversions */
  331. nosign:            sign = '\0';
  332.             /*
  333.              * ``... diouXx conversions ... if a precision is
  334.              * specified, the 0 flag will be ignored.''
  335.              *    -- ANSI X3J11
  336.              */
  337. number:            if ((dprec = prec) >= 0)
  338.                 flags &= ~ZEROPAD;
  339.  
  340.             /*
  341.              * ``The result of converting a zero value with an
  342.              * explicit precision of zero is no characters.''
  343.              *    -- ANSI X3J11
  344.              */
  345.             t = buf + BUF;
  346.             if (_ulong != 0 || prec != 0) {
  347.                 do {
  348.                     *--t = digs[_ulong % base];
  349.                     _ulong /= base;
  350.                 } while (_ulong);
  351.                 digs = "0123456789abcdef";
  352.                 if (flags & ALT && base == 8 && *t != '0')
  353.                     *--t = '0'; /* octal leading 0 */
  354.             }
  355.             size = buf + BUF - t;
  356.  
  357. pforw:
  358.             /*
  359.              * All reasonable formats wind up here.  At this point,
  360.              * `t' points to a string which (if not flags&LADJUST)
  361.              * should be padded out to `width' places.  If
  362.              * flags&ZEROPAD, it should first be prefixed by any
  363.              * sign or other prefix; otherwise, it should be blank
  364.              * padded before the prefix is emitted.  After any
  365.              * left-hand padding and prefixing, emit zeroes
  366.              * required by a decimal [diouxX] precision, then print
  367.              * the string proper, then emit zeroes required by any
  368.              * leftover floating precision; finally, if LADJUST,
  369.              * pad with blanks.
  370.              */
  371.  
  372.             /*
  373.              * compute actual size, so we know how much to pad
  374.              * fieldsz excludes decimal prec; realsz includes it
  375.              */
  376.             fieldsz = size + fpprec;
  377.             if (sign)
  378.                 fieldsz++;
  379.             if (flags & HEXPREFIX)
  380.                 fieldsz += 2;
  381.             realsz = dprec > fieldsz ? dprec : fieldsz;
  382.  
  383.             /* right-adjusting blank padding */
  384.             if ((flags & (LADJUST|ZEROPAD)) == 0 && width)
  385.                 for (n = realsz; n < width; n++)
  386.                     PUTC(' ');
  387.             /* prefix */
  388.             if (sign)
  389.                 PUTC(sign);
  390.             if (flags & HEXPREFIX) {
  391.                 PUTC('0');
  392.                 PUTC((char)*fmt);
  393.             }
  394.             /* right-adjusting zero padding */
  395.             if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
  396.                 for (n = realsz; n < width; n++)
  397.                     PUTC('0');
  398.             /* leading zeroes from decimal precision */
  399.             for (n = fieldsz; n < dprec; n++)
  400.                 PUTC('0');
  401.  
  402.             /* the string or number proper */
  403.             if (fp->_cnt - (n = size) >= 0 &&
  404.                 (fp->_flag & _IOLBF) == 0) {
  405.                 fp->_cnt -= n;
  406.                 bcopy(t, (char *)fp->_ptr, n);
  407.                 fp->_ptr += n;
  408.             } else
  409.                 while (--n >= 0)
  410.                     PUTC(*t++);
  411.             /* trailing f.p. zeroes */
  412.             while (--fpprec >= 0)
  413.                 PUTC('0');
  414.             /* left-adjusting padding (always blank) */
  415.             if (flags & LADJUST)
  416.                 for (n = realsz; n < width; n++)
  417.                     PUTC(' ');
  418.             /* finally, adjust cnt */
  419.             cnt += width > realsz ? width : realsz;
  420.             break;
  421.         case '\0':    /* "%?" prints ?, unless ? is NULL */
  422.             return (cnt);
  423.         default:
  424.             PUTC((char)*fmt);
  425.             cnt++;
  426.         }
  427.     }
  428.     /* NOTREACHED */
  429. }
  430.  
  431. static int
  432. cvt(number, prec, flags, signp, fmtch, startp, endp)
  433.     double number;
  434.     register int prec;
  435.     int flags;
  436.     u_char fmtch;
  437.     char *signp, *startp, *endp;
  438. {
  439.     register char *p, *t;
  440.     register double fract;
  441.     int dotrim, expcnt, gformat;
  442.     double integer, tmp, modf();
  443.  
  444.     /* JF for numbers */
  445.     if(number!=number) {
  446.         t= ++startp;
  447.         p=__nan_str;
  448.         while(*p)
  449.             *t++= *p++;
  450.         return t-startp;
  451.     }
  452.     if(number==__plinf) {
  453.         t= ++startp;
  454.         p= (*signp) ? __ninf_str : __inf_str;
  455.         *signp=0;
  456.         while(*p)
  457.             *t++= *p++;
  458.         return t-startp;
  459.     }
  460.     /* if(number==__neinf) {
  461.         t= ++startp;
  462.         p=__ninf_str;
  463.         while(*p)
  464.             *t++= *p++;
  465.         return t-startp;
  466.     } */
  467.     dotrim = expcnt = gformat = 0;
  468.     fract = modf(number, &integer);
  469.  
  470.     /* get an extra slot for rounding. */
  471.     t = ++startp;
  472.  
  473.     /*
  474.      * get integer portion of number; put into the end of the buffer; the
  475.      * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
  476.      */
  477.     for (p = endp - 1; integer; ++expcnt) {
  478.         tmp = modf(integer / 10, &integer);
  479.         *p-- = tochar((int)((tmp + .01) * 10));
  480.     }
  481.     switch(fmtch) {
  482.     case 'f':
  483.         /* reverse integer into beginning of buffer */
  484.         if (expcnt)
  485.             for (; ++p < endp; *t++ = *p);
  486.         else
  487.             *t++ = '0';
  488.         /*
  489.          * if precision required or alternate flag set, add in a
  490.          * decimal point.
  491.          */
  492.         if (prec || flags&ALT)
  493.             *t++ = '.';
  494.         /* if requires more precision and some fraction left */
  495.         if (fract) {
  496.             if (prec)
  497.                 do {
  498.                     fract = modf(fract * 10, &tmp);
  499.                     *t++ = tochar((int)tmp);
  500.                 } while (--prec && fract);
  501.             if (fract)
  502.                 startp = round(fract, (int *)NULL, startp,
  503.                     t - 1, (char)0, signp);
  504.         }
  505.         for (; prec--; *t++ = '0');
  506.         break;
  507.     case 'e':
  508.     case 'E':
  509. eformat:    if (expcnt) {
  510.             *t++ = *++p;
  511.             if (prec || flags&ALT)
  512.                 *t++ = '.';
  513.             /* if requires more precision and some integer left */
  514.             for (; prec && ++p < endp; --prec)
  515.                 *t++ = *p;
  516.             /*
  517.              * if done precision and more of the integer component,
  518.              * round using it; adjust fract so we don't re-round
  519.              * later.
  520.              */
  521.             if (!prec && ++p < endp) {
  522.                 fract = 0;
  523.                 startp = round((double)0, &expcnt, startp,
  524.                     t - 1, *p, signp);
  525.             }
  526.             /* adjust expcnt for digit in front of decimal */
  527.             --expcnt;
  528.         }
  529.         /* until first fractional digit, decrement exponent */
  530.         else if (fract) {
  531.             /* adjust expcnt for digit in front of decimal */
  532.             for (expcnt = -1;; --expcnt) {
  533.                 fract = modf(fract * 10, &tmp);
  534.                 if (tmp)
  535.                     break;
  536.             }
  537.             *t++ = tochar((int)tmp);
  538.             if (prec || flags&ALT)
  539.                 *t++ = '.';
  540.         }
  541.         else {
  542.             *t++ = '0';
  543.             if (prec || flags&ALT)
  544.                 *t++ = '.';
  545.         }
  546.         /* if requires more precision and some fraction left */
  547.         if (fract) {
  548.             if (prec)
  549.                 do {
  550.                     fract = modf(fract * 10, &tmp);
  551.                     *t++ = tochar((int)tmp);
  552.                 } while (--prec && fract);
  553.             if (fract)
  554.                 startp = round(fract, &expcnt, startp,
  555.                     t - 1, (char)0, signp);
  556.         }
  557.         /* if requires more precision */
  558.         for (; prec--; *t++ = '0');
  559.  
  560.         /* unless alternate flag, trim any g/G format trailing 0's */
  561.         if (gformat && !(flags&ALT)) {
  562.             while (t > startp && *--t == '0');
  563.             if (*t == '.')
  564.                 --t;
  565.             ++t;
  566.         }
  567.         t = exponent(t, expcnt, fmtch);
  568.         break;
  569.     case 'g':
  570.     case 'G':
  571.         /* a precision of 0 is treated as a precision of 1. */
  572.         if (!prec)
  573.             ++prec;
  574.         /*
  575.          * ``The style used depends on the value converted; style e
  576.          * will be used only if the exponent resulting from the
  577.          * conversion is less than -4 or greater than the precision.''
  578.          *    -- ANSI X3J11
  579.          */
  580.         if (expcnt > prec || !expcnt && fract && fract < .0001) {
  581.             /*
  582.              * g/G format counts "significant digits, not digits of
  583.              * precision; for the e/E format, this just causes an
  584.              * off-by-one problem, i.e. g/G considers the digit
  585.              * before the decimal point significant and e/E doesn't
  586.              * count it as precision.
  587.              */
  588.             --prec;
  589.             fmtch -= 2;        /* G->E, g->e */
  590.             gformat = 1;
  591.             goto eformat;
  592.         }
  593.         /*
  594.          * reverse integer into beginning of buffer,
  595.          * note, decrement precision
  596.          */
  597.         if (expcnt)
  598.             for (; ++p < endp; *t++ = *p, --prec);
  599.         else
  600.             *t++ = '0';
  601.         /*
  602.          * if precision required or alternate flag set, add in a
  603.          * decimal point.  If no digits yet, add in leading 0.
  604.          */
  605.         if (prec || flags&ALT) {
  606.             dotrim = 1;
  607.             *t++ = '.';
  608.         }
  609.         else
  610.             dotrim = 0;
  611.         /* if requires more precision and some fraction left */
  612.         if (fract) {
  613.             if (prec) {
  614.                 do {
  615.                     fract = modf(fract * 10, &tmp);
  616.                     *t++ = tochar((int)tmp);
  617.                 } while(!tmp);
  618.                 while (--prec && fract) {
  619.                     fract = modf(fract * 10, &tmp);
  620.                     *t++ = tochar((int)tmp);
  621.                 }
  622.             }
  623.             if (fract)
  624.                 startp = round(fract, (int *)NULL, startp,
  625.                     t - 1, (char)0, signp);
  626.         }
  627.         /* alternate format, adds 0's for precision, else trim 0's */
  628.         if (flags&ALT)
  629.             for (; prec--; *t++ = '0');
  630.         else if (dotrim) {
  631.             while (t > startp && *--t == '0');
  632.             if (*t != '.')
  633.                 ++t;
  634.         }
  635.     }
  636.     return(t - startp);
  637. }
  638.  
  639. static char *
  640. round(fract, exp, start, end, ch, signp)
  641.     double fract;
  642.     int *exp;
  643.     register char *start, *end;
  644.     char ch, *signp;
  645. {
  646.     double tmp;
  647.  
  648.     if (fract)
  649.         (void)modf(fract * 10, &tmp);
  650.     else
  651.         tmp = todigit(ch);
  652.     if (tmp > 4)
  653.         for (;; --end) {
  654.             if (*end == '.')
  655.                 --end;
  656.             if (++*end <= '9')
  657.                 break;
  658.             *end = '0';
  659.             if (end == start) {
  660.                 if (exp) {    /* e/E; increment exponent */
  661.                     *end = '1';
  662.                     ++*exp;
  663.                 }
  664.                 else {        /* f; add extra digit */
  665.                     *--end = '1';
  666.                     --start;
  667.                 }
  668.                 break;
  669.             }
  670.         }
  671.     /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
  672.     else if (*signp == '-')
  673.         for (;; --end) {
  674.             if (*end == '.')
  675.                 --end;
  676.             if (*end != '0')
  677.                 break;
  678.             if (end == start)
  679.                 *signp = 0;
  680.         }
  681.     return(start);
  682. }
  683.  
  684. static char *
  685. exponent(p, exp, fmtch)
  686.     register char *p;
  687.     register int exp;
  688.     u_char fmtch;
  689. {
  690.     register char *t;
  691.     char expbuf[MAXEXP];
  692.  
  693.     *p++ = fmtch;
  694.     if (exp < 0) {
  695.         exp = -exp;
  696.         *p++ = '-';
  697.     }
  698.     else
  699.         *p++ = '+';
  700.     t = expbuf + MAXEXP;
  701.     if (exp > 9) {
  702.         do {
  703.             *--t = tochar(exp % 10);
  704.         } while ((exp /= 10) > 9);
  705.         *--t = tochar(exp);
  706.         for (; t < expbuf + MAXEXP; *p++ = *t++);
  707.     }
  708.     else {
  709.         *p++ = '0';
  710.         *p++ = tochar(exp);
  711.     }
  712.     return(p);
  713. }
  714.